// pages/report/report.js
var util = require('../../utils/util.js');
Page({
    data: {
        areaIndex: 0,
        taskIndex: 0,
        task: ['C级', 'B级', 'A级'],
        a: -1,
        b: -1,
        placeholder:"请输入备注",
        fixed:false
    },
    //选择器
    bindPickerChange: function(e) {
        var num = e.detail.value;
        var indexs = this.data.infonum[num]
        this.setData({
            areaIndex: indexs
        })
    },
    bindPickertaskChange: function(e) {
        this.setData({
            taskIndex: e.detail.value
        })
    },
    bindareatap: function() {
        var num = 1
        this.setData({
            a: num
        })
    },
    bindtasktap: function() {
        var num = 1
        this.setData({
            b: num
        })

    },
    /*
     * 生命周期函数--监听页面加载
     */
    onLoad: function() {
        this.getHall();
        //调用函数时，传入new Date()参数，返回值是日期和时间  
        var time = util.formatTime1(new Date());
        // 再通过setData更改Page()里面的data，动态更新页面的数据  
        this.setData({
            time: time
        })
    },
    // 获取展馆
    getHall: function() {
        util.myrequest('', '/hall/lists', 'GET', (res) => {
            if (res.result) {
                var name = [];
                var infonum = [];
                for (var i = 0; i < res.datas.length; i++) {
                    name[i] = res.datas[i].name;
                    infonum[i] = res.datas[i].id;
                }
                this.setData({
                    area: name,
                    infonum: infonum
                })
            }
        })
    },
    // 选择图片
    chooseImg: function() {
        wx.chooseImage({
            // 上传成功
            success: (res) => {
                // 图片上传中
                wx.showLoading({
                    title: '图片上传中',
                    mask: true
                })
                let images = [];
                let imgSrc = "";
                var tempFilePaths = res.tempFilePaths; //图片文件路径
                for (let i = 0; i < tempFilePaths.length; i++) {
                    this.uploadImage(images, tempFilePaths[i], i);
                }
                // 轮询异步加载完成后赋值
                let lunxun = setInterval(() => {
                    if (images.length >= tempFilePaths.length) {
                        imgSrc = images.join(",");
                        clearInterval(lunxun);
                        this.setData({
                            images: imgSrc,
                            imagesrc: res.tempFilePaths,
                        })
                        wx.hideLoading()
                    }
                }, 1000)
            },
            complete: () => {},
        })
    },
    // 上传图片单张
    uploadImage: function(images, tempFilePaths, i) {
        var token = wx.getStorageSync('token');
        var account = wx.getStorageSync('account');
        const uploadTask = wx.uploadFile({
            url: 'https://bwgxcxapitest.taoomall.com/report/upload',
            filePath: tempFilePaths,
            header: { 'ng-params-one': account, 'ng-params-two': token },
            name: 'image',
            success: function(res) {
                // 后台返回图片路径
                images[i] = res.data;
            },
            fail: function(res) {}
        })
    },
    //监听事件
    listenerpersonInput: function(e) {
        this.setData({
            visitorno: e.detail.value
        })
    },
    listenertipsInput: function(e) {
        this.setData({
            tips: e.detail.value

        })
    },
    // 上报
    formSubmit: function() {
        var arr = {
            hallid: this.data.areaIndex,
            visitorno: this.data.visitorno,
            tips: this.data.tips,
            level: this.data.taskIndex,
            images: this.data.images
        }
        util.myrequest(arr, '/report/add', 'POST', (res) => {
            if (res.result) {
                wx.showToast({
                    title: '上报成功',
                    icon: 'loading',
                    duration: 1000
                })
                setTimeout(function() {
                    wx.switchTab({
                        url: '../notice/notice'
                    })
                }, 1000)
            } else {
                wx.showToast({
                    title: '上报失败',
                    icon: 'loading',
                    duration: 1000
                })
            }
        })
    },
    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    },
})